/*
 * @Author: WuFeng <763467339@qq.com>
 * @Date: 2024-10-10 14:38:28
 * @LastEditTime: 2024-10-21 21:19:20
 * @LastEditors: WuFeng <763467339@qq.com>
 * @Description:
 * @FilePath: \markdown-magic\src-tauri\src\lib.rs
 * Copyright 版权声明
 */
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[cfg(desktop)]
mod tray;

#[tauri::command]
fn my_custom_command(window: tauri::Window) {
    println!("Window: {}", window.label());
}

#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_global_shortcut::Builder::new().build())
        .plugin(tauri_plugin_window_state::Builder::new().build())
        // 文件系统
        .plugin(tauri_plugin_fs::init())
        // 对话框
        .plugin(tauri_plugin_dialog::init())
        // 访问系统 shell
        .plugin(tauri_plugin_shell::init())
        // 从前端调rust
        .invoke_handler(tauri::generate_handler![greet, my_custom_command])
        .setup(|app| {
            #[cfg(all(desktop))]
            {
                // 托盘
                let handle = app.handle();
                tray::create_tray(handle)?;

                // // 全局快捷键注册 TODO: 监听快捷键方案一
                // use tauri::Emitter;
                // use tauri_plugin_global_shortcut::{Code, Modifiers, ShortcutState};
                // app.handle().plugin(
                //     tauri_plugin_global_shortcut::Builder::new()
                //         .with_shortcuts(["ctrl+alt+o", "ctrl+alt+k", "ctrl+s", "ctrl+alt+s"])?
                //         .with_handler(|app, shortcut, event| {
                //             if event.state == ShortcutState::Pressed {
                //                 // ctrl+alt+o 打开文件
                //                 if shortcut.matches(Modifiers::ALT | Modifiers::CONTROL, Code::KeyO)
                //                 {
                //                     let _ = app.emit("shortcut-event", "ctrl+alt+o");
                //                 }
                //                 // ctrl+alt+k 打开文件夹
                //                 if shortcut.matches(Modifiers::ALT | Modifiers::CONTROL, Code::KeyK)
                //                 {
                //                     let _ = app.emit("shortcut-event", "ctrl+alt+k");
                //                 }
                //                 // ctrl+s 保存
                //                 if shortcut.matches(Modifiers::CONTROL, Code::KeyS) {
                //                     let _ = app.emit("shortcut-event", "ctrl+s");
                //                 }
                //                 // ctrl+alt+s 另存为
                //                 if shortcut.matches(Modifiers::ALT | Modifiers::CONTROL, Code::KeyS)
                //                 {
                //                     let _ = app.emit("shortcut-event", "ctrl+alt+s");
                //                 }
                //             }
                //         })
                //         .build(),
                // )?;
            }
            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
